package praca.gra;

import java.util.List;

import praca.framework.TextureGL;
import praca.framework.Vector;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Screen;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class Ex06Vectors extends Screen{

	Vertices vert;
	Vertices trollVert;
	Vector touchPos;
	Vector rectanglePos;
	TouchEvent ev;
	float angle = 0;
	Texture troll;
	Vector trollPos = new Vector(0,0);
	Vector trollVelocity = new Vector(0,0);
	Vector gravity = new Vector(-30,-400);	//-400 to grawitacja dobrana eksperymentalnie
											//a -30 to wiatr :)
	float trollRotation = 0;
	
	
	public Ex06Vectors(Game game) {
		super(game);
		vert = new Vertices(50,200,false);
		trollVert = new Vertices(30,30,true);
		rectanglePos = new Vector(0, 0);
		touchPos = new Vector(0,0);
		troll = new TextureGL(game, "troll.png");
		g.setTextureTransparentBackground();
	}

	@Override
	public void update(float deltaTime) {
		g.clearTransformations();
		
		//szybkosc obrotu zalezy od aktualnej predkosci
		trollRotation+=trollVelocity.len()*0.05;
		if(trollRotation >= 360)
			trollRotation=0;
		
		if(!in.getTouchEvents().isEmpty()){
			ev = in.getTouchEvents().remove(0);
			if(ev.getType() == TouchEvent.TOUCH_MOVE || ev.getType() == TouchEvent.TOUCH_DOWN){
				touchPos.setXY(ev.getX(), ev.getY());
				
				/*aby obliczyc kat miedzy punktem dotyku a narysowanym prostokatem
				 * odejmujemy wspolrzedne dotyku i prostokata otrzymujac tym samym wektor odleglosci
				 * wywolujac na tym metodzie angle otrzymujemy kat miedzy osia x
				 * a tym wektorek
				 */
				angle = touchPos.sub(rectanglePos).angle();
				//Log.d("touch", "touch x: "+ev.getX()+" ,y: "+ev.getY());
			}else
			
			if(ev.getType() == TouchEvent.TOUCH_UP){
				
				touchPos.setXY(ev.getX(), ev.getY());
				angle = touchPos.sub(rectanglePos).angle();
			
				float radians = angle * Vector.TO_RADIANS;
				trollPos.setXY(rectanglePos.x, rectanglePos.y);
				float trollSpeed = 600;
				
				Log.d("Velocity", "TouchPos.length: "+trollVelocity.len());
				Log.d("Velocity", "Angle: "+angle);
				float length = trollVelocity.len();
				
				/* Strona 360 w pdfie */
				trollVelocity.x = (float) (Math.cos(radians)* trollSpeed);// (float) (Math.cos(radians) * trollSpeed);
				trollVelocity.y = (float) (Math.sin(radians)* trollSpeed); //(float) (Math.sin(radians) * trollSpeed);
				
				
				
				//Log.d("touch", "touch x: "+ev.getX()+" ,y: "+ev.getY());
			}
		}
		trollVelocity.add(gravity.x * deltaTime, gravity.y * deltaTime);
		trollPos.add(trollVelocity.x * deltaTime, trollVelocity.y * deltaTime);
		//Log.d("Velocity","vel.x: "+trollVelocity.x+" ,vel.y: "+trollVelocity.y);
		Log.d("Velocity", "Velocity.length: "+trollVelocity.len());
		g.drawColor(0, 0.2f, 1);
		
		g.moveTexture(trollPos.x, trollPos.y);
		g.rotateTexture(trollRotation);
		g.drawTexture(troll, trollVert);
		
		g.clearTransformations();
		
		g.rotateTexture(angle+90);
		g.drawRect(vert);
		
		
		
		
	}



	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}


}
